package net.bplaced.streetfighter;

import java.util.ArrayList;

import net.bplaced.streetfighter.adapter.GoalGetterAdapter;
import net.bplaced.streetfighter.lib.async.task.GoalGetterLoaderTask;
import net.bplaced.streetfighter.lib.model.GoalGetter;
import net.bplaced.streetfighter.lib.utils.CallBack;
import net.bplaced.streetfighter.lib.utils.ServiceReturnPackage;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.Toast;

/**
 * @author Sebastian Hoffmann <bo.hoffi@googlemail.com>
 */
public class GoalGetterActivity
        extends Activity {

    private GoalGetterAdapter goalGetterAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.goalgetter);

        final ListView list = (ListView) this.findViewById(R.id.goalgetter_list);
        list.setDividerHeight(0);

        this.goalGetterAdapter = new GoalGetterAdapter(this, new ArrayList<GoalGetter>());

        list.setAdapter(this.goalGetterAdapter);

        final GoalGetterLoaderTask task =
                new GoalGetterLoaderTask(this, this.getResources().getString(R.string.goalgetter_progress_message),
                        this.onFinishedLoading);
        task.execute();
    }

    /**
     * Deklariert einen Callback, welcher zum Ende des Serviceaurufes aufgerufen wird.
     */
    private final CallBack<ServiceReturnPackage<GoalGetter[]>> onFinishedLoading =
            new CallBack<ServiceReturnPackage<GoalGetter[]>>() {

                @Override
                public void callbackAction(ServiceReturnPackage<GoalGetter[]> result) {

                    if (result != null) {
                        if (result.getContent() != null) {

                            GoalGetterActivity.this.goalGetterAdapter.addAll(result.getContent());

                        } else {

                            Toast.makeText(GoalGetterActivity.this, result.getErrorMessage(), Toast.LENGTH_SHORT)
                                    .show();
                        }
                    }
                };
            };
}